<?php
/**
 * 支付服务层
 * @author maniac
 */

namespace app\common\service;

use app\common\model\OrderBank;
use app\common\model\WechatUsers AS WechatUsersModel;
use app\common\model\OrderBank AS OrderBankModel;
use app\common\model\Members AS MembersModel;
use app\facade\Wechat AS WechatFacade;
use app\facade\Site AS SiteFacade;
use app\facade\Alipay as AlipayFacade;

class Pay extends Base
{
    //请求支付
    public function repay($data){
        // 微信用户信息
        $user        = WechatUsersModel::field('openid,nickname')->where('member_id', $data['userid'])->find();
        // 会员信息
//        $member      = MembersModel::field('id,role_id,points,balance')->where('id', $data['userid'])->find();
        //订单信息
        $ordre      = OrderBankModel::field('id,pay_type,pay_status,pay_amount,bankpostid,pay_yue,pid')->where('bankpostid', $data['id'])->find();
        if(!$ordre){
            exception('此订单不存在');
        }
        if($ordre['pay_status'] == 1){
            exception('此订单已支付');
        }
        $orderAmount = $ordre['pay_amount'];
        $data['pay_amount']=$ordre['pay_amount'];
        $data['pay_yue']=0;
//        if ($data['pay_type'] == 1) {
            // 微信支付
            $request = [
                'out_trade_no' => $ordre['bankpostid'],
                'openid'       => $user['openid'],
                'pay_amount'   => $ordre['pay_amount'],
                'platform'   => $data['platform'],
            ];
            $wechat = WechatFacade::unifiedorder($request);
            $result['wxpay'] = [
                'timeStamp' => $wechat['timeStamp'],
                'nonceStr'  => $wechat['nonceStr'],
                'package'   => $wechat['package'],
                'paySign'   => $wechat['paySign'],
            ];

//        } elseif ($data['pay_type'] == 2) {
//            // 支付宝
//            $request = [
//                'out_trade_no' => $ordre['bankpostid'],
//                'pay_amount'   => $ordre['pay_amount'],
//            ];
//            $result['wxpay']  = AlipayFacade::requestPay($request);
//        } elseif ($data['pay_type'] == 3) {
//            // 积分支付
//            $pointSetting = SiteFacade::pointsSetting();
//            // 计算该订单需要多少积分支付
//            $needPoints   = $orderAmount * $pointSetting['points_rate'];
//            if ($orderAmount < $pointSetting['points_buy_limit']) {
//                exception('金额必须大于'.$pointSetting['points_buy_limit'].'元');
//            }
//            if ($needPoints && $needPoints > $member['points']) {
//                exception('积分不足');
//            }
//            $data['points'] = $needPoints;
//        } elseif ($data['pay_type'] == 4) {
//            // 股东抵用券支付
//            $vouchersSetting = SiteFacade::vouchersSetting();
//            // 计算该订单需要多少抵用券支付
//            $needVouchers    = $orderAmount * $vouchersSetting['vouchers_rate'];
//            if ($orderAmount < $vouchersSetting['vouchers_buy_limit']) {
//                exception('金额必须大于'.$vouchersSetting['vouchers_buy_limit'].'元');
//            }
//            if ($needVouchers && $needVouchers > $member['vouchers']) {
//                exception('抵用券不足');
//            }
//            $data['vouchers'] = $needVouchers;
//        } elseif ($data['pay_type'] == 5) {
//            // 余额支付
//            $data['pay_yue'] = $orderAmount;
//            if ($data['pay_yue'] && $data['pay_yue'] > $member['balance']) {
//                exception('余额不足');
//            }
//
//            $data['pay'] = 'ok';
//            $notify = array(
//                'out_trade_no' => $ordre['bankpostid'],
//                'transaction_id' => '',
//            );
//            \app\common\model\Orders::update([
//                'id'=>$ordre['pid'],
//                'pay_type'=>$data['pay_type'],
//                'balance'=>$data['pay_amount'],
//            ]);
//            $ordre->pay_amount = $data['pay_amount'];
//            $ordre->pay_yue = $data['pay_yue'];
//            $ordre->pay_type = $data['pay_type'];
//            $ordre->save();
//
//            WechatFacade::notify($notify);
//
//            return $data;
//
//        } elseif ($data['pay_type'] == 6) {
//            // 余额 加 微信 混合支付
//
//            $data['pay_yue'] = $member['balance'];
//            $data['pay_amount'] = $ordre['pay_amount'] - $data['pay_yue'];
//            $request = [
//                'out_trade_no' => $ordre['bankpostid'],
//                'openid'       => $user['openid'],
//                'pay_amount'   => $data['pay_amount'],
//                'platform'   => $data['platform'],
//            ];
//            $wechat = WechatFacade::unifiedorder($request);
//            $result['wxpay'] = [
//                'timeStamp' => $wechat['timeStamp'],
//                'nonceStr'  => $wechat['nonceStr'],
//                'package'   => $wechat['package'],
//                'paySign'   => $wechat['paySign'],
//            ];
//
//
//        }elseif ($data['pay_type'] == 7) {
//            // 余额 加 支付宝 混合支付
//            $data['pay_yue'] = $member['balance'];
//            $data['pay_amount'] = $ordre['pay_amount'] - $data['pay_yue'];
//        }
        $ordre->pay_amount = $data['pay_amount'];
        $ordre->pay_yue = $data['pay_yue'];
        $ordre->pay_type = $data['pay_type'];

        $ordre->save();

        return $result;

    }


}
